package com.artisan.service.admin.impl;

import com.artisan.common.pojo.PageBean;
import com.artisan.common.pojo.PageParam;
import com.artisan.dao.UserMapper;
import com.artisan.dao.UserRoleMapper;
import com.artisan.medium.admin.service.UserService;
import com.artisan.model.User;
import com.artisan.model.UserExample;
import com.artisan.model.UserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/1.
 */
@Service("userService")
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public List<Map<String, String>> searchUserAuth(int id) {
        return userMapper.searchUserAuth(id);
    }

    @Override
    public User checkLogin(String username, String password) {

        UserExample userExample=new UserExample();
        UserExample.Criteria criteria=userExample.createCriteria();
        criteria.andNickNameEqualTo(username);
        criteria.andPasswordEqualTo(password);
        List<User>userExampleList=userMapper.selectByExample(userExample);

       if(userExampleList.size()!=0){
           User user= userExampleList.get(0);
           UserRole userRole=userRoleMapper.selectByPrimaryKey(user.getRoleId());
           user.setRoleName(userRole.getrName());
           return  user;
       }else{
           return null;
       }
    }

    @Override
    public PageBean listPage(PageParam pageParam, Map<String, Object> paramMap) {
        // 根据页面传来的分页参数构造SQL分页参数
        paramMap.put("pageFirst", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage());
        paramMap.put("pageSize", pageParam.getNumPerPage());
        paramMap.put("startRowNum", (pageParam.getPageNum() - 1) * pageParam.getNumPerPage());
        paramMap.put("endRowNum", pageParam.getPageNum() * pageParam.getNumPerPage());

        // 统计总记录数
        Long count = userMapper.listPageCount(paramMap);

        // 获取分页数据集
        List<Object> list = userMapper.listPage(paramMap);

         // 构造分页对象
         return new PageBean(pageParam.getPageNum(), pageParam.getNumPerPage(), count.intValue(), list);
    }
}
